﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LowLevelGraphics.Filter.Contours
{
    /// <summary>
    /// This covers the direction like found in a java example
    /// So don't use an enum to specify a direction, but use THIS
    /// </summary>
    public class Direction : IEnumerable
    {
        // statics
        public static Direction E = new Direction(1, 0);
        public static Direction NE = new Direction(1, 1);
        public static Direction N = new Direction(0, 1);
        public static Direction NW = new Direction(-1, 1);
        public static Direction W = new Direction(-1, 0);
        public static Direction SW = new Direction(-1, -1);
        public static Direction S = new Direction(0, -1);
        public static Direction SE = new Direction(1, -1);

        protected Direction[] m_aDirection = new Direction[] { E, NE, N, NW, W, SW, S, SE };
        // fields

        /// <summary>
        /// The horizontal distance moved in this direction within the plane.
        /// </summary>
        public int planeX;

        /// <summary>
        /// The vertical distance moved in this direction within the plane.
        /// </summary>
        public int planeY;

        /// <summary>
        /// The horizontal distance moved in this direction in screen coordinates.
        /// </summary>
        public int screenX;

        /// <summary>
        ///  The vertical distance moved in this direction in screen coordinates.
        /// </summary>
        public int screenY;

        /// <summary>
        ///  The euclidean length of this direction's vectors.
        /// </summary>
        public double length;

        // constructor
        public Direction()
        {
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Direction"/> class.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        protected Direction(int x, int y)
        {
            planeX = x;
            planeY = y;
            screenX = x;
            screenY = -y;
            length = x != 0 && y != 0 ? Math.Sqrt(2.0) / 2.0 : 1.0;
        }

        #region IEnumerable Members

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator GetEnumerator()
        {
            return this.m_aDirection.GetEnumerator();
        }

        #endregion
    }
}
